package com.nexgo.payment.trans.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.trans.setting.SystemSettingsActivity;
import com.nexgo.payment.trans.setting.transmanage.SetupOtherSwitchActivity;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;
import com.nexgo.sound.SoundManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 签到界面
 */
public class LoginActivity extends AbsTransActivity<LoginContract.Presenter> implements LoginContract.View {
    public final static String FORCE = "force";

    @BindView(R.id.et_operator_no)
    EditText etUserNo;
    @BindView(R.id.et_password)
    EditText etPassword;
    private boolean mForceLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        Bundle args = getIntent().getExtras();
        if (args != null) {
            mForceLogin = args.getBoolean(FORCE, false);
        }
        initTitle();
//        getWindow().addFlags(3);  //屏蔽home键
//        getWindow().addFlags(5);  //屏蔽菜单键
        new LoginPresenter(this, Repository.getInstance());
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setTitleText(getString(R.string.trans_login))
                .setLeftImage(mForceLogin ? 0 : R.drawable.arrow_icon)
                .setRightImage(R.drawable.setting_icon)
                .setRightOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(Settings.ACTION_SETTINGS));
                    }
                })
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @OnClick({R.id.btn_login, R.id.title_iv_left})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_login:
                mPresenter.login(etUserNo.getText().toString(), etPassword.getText().toString());
                break;
            case R.id.title_iv_left:
                quitLogin();
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            //拦截返回键，禁用返回按钮
            if (keyCode == KeyEvent.KEYCODE_BACK && mForceLogin) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, LoginActivity.class);
    }

    public static Intent newInstance(Context context, boolean force) {
        Intent intent = new Intent(context, LoginActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(FORCE, force);
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    public void showOperatorEmpty() {
        ToastUtils.showToast(getString(R.string.input_operator_no_can_no_be_empty));
    }

    @Override
    public void showPasswordEmpty() {
        ToastUtils.showToast(getString(R.string.input_password_can_no_be_empty));
    }

    @Override
    public void showOperatorNotExist() {
        etUserNo.setText("");
        etPassword.setText("");
        ToastUtils.showToast(getString(R.string.operator_not_exist));
    }

    @Override
    public void showPasswordError() {
        etPassword.setText("");
        ToastUtils.showToast(getString(R.string.operator_password_error));
    }

    @Override
    public void login2SystemSetting() {
        startActivity(SystemSettingsActivity.newInstance(this));
        etUserNo.setText("");
        etPassword.setText("");
    }
    @Override
    public void login3SystemSetting() {
        Bundle b=new Bundle();
        b.putBoolean("isshow",true);
        intent2Activity(SetupOtherSwitchActivity.class,b);
        etUserNo.setText("");
        etPassword.setText("");
    }

    @Override
    public void showSuccessfullyLogin() {
        ToastUtils.showToast("签到成功");
        quitLogin();
    }

    @Override
    public void quitLogin() {
        finish();
    }

    @Override
    public void showError(String message) {
        SoundManager soundManager = new SoundManager();
        soundManager.play(SoundManager.Type.TRANS_FAIL);
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WRONG);
        dialogFragment.setTitleText(getString(R.string.tip));
        dialogFragment.setContentText(message, Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDownTimer(SysConstant.TIME_OUT_10, true);
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {}

            @Override
            public void onConfirm() {
                soundManager.stop();
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    public void quitTrans() {
        //屏蔽父类调用退出签到界面
        //super.quitTrans();
    }
}
